package com.idealighter.game.games.fqzs.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.games.fqzs.message.ReqBetMsg;
import com.idealighter.game.games.fqzs.message.ResBetStageMsg;
import com.idealighter.game.message.ModuleMsgIdConstant;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(ModuleMsgIdConstant.Fqzs.RES_BET_STAGE)
public class ResBetStageHandler implements ResMessageHandler {

  private static final Logger LOG = LoggerFactory.getLogger(ResBetStageHandler.class);

  private static final List<BetItem> betList = Arrays.asList(new BetItem(1000L, 40L), // 下注1k
      new BetItem(10000L, 80L), // 下注1w
      new BetItem(100000L, 80L), // 下注10w
      new BetItem(1000000L, 40L), // 下注100w
      new BetItem(1000000L, 20L) // 下注1000w
  );
  private static long betItemTotalProbs = 0L;

  static {
    for (BetItem item : betList) {
      betItemTotalProbs += item.getProbs();
    }
  }

  @Override
  public void action(Player player, ResMessage message) {
    ResBetStageMsg msg = (ResBetStageMsg) message;
    // 筹码在进入桌子时兑换，游戏结算时增加/减少筹码和金币
    if (player.playerInfo.getGold() <= 0) {
      player.delayLogout();
      return;
    }

    int betStageTime = msg.getTime() - 1;
    /*
     * 在下注时间段内，随机5至10次下注
     */
    for (int i = 0; i < RandCodeUtil.random(5, 10); i++) {
      player.schedule(() -> doBet(player), RandCodeUtil.random(1, betStageTime), TimeUnit.SECONDS);
    }
  }

  /**
   * 下注.
   * 
   * @param player .
   */
  private void doBet(Player player) {
    MemInfo member = player.member(player.playerId);
    long bet = 0;
    if (member != null) {
      BetItem betItem = randBetItem();
      bet = betItem.getChips();
      if (bet <= member.getChips()) {
        ReqBetMsg msg = new ReqBetMsg();
        msg.setIcon(RandCodeUtil.random(1, 11));
        msg.setBet(bet);
        player.sendMsg(msg);

        LOG.info("[飞禽走兽]玩家[{}][{}]发起下注：图标[{}]筹码[{}]", player.userName, player.playerId,
            msg.getIcon(), msg.getBet());
      }
    }
  }

  private BetItem randBetItem() {
    long randValue = RandCodeUtil.random(betItemTotalProbs);
    BetItem result = null;
    for (BetItem item : betList) {
      randValue -= item.getProbs();
      if (randValue <= 0L) {
        result = item;
        break;
      }
    }
    return result;
  }

  static class BetItem {
    private long chips; // 下注金币
    private long probs; // 权值

    public BetItem(long chips, long probs) {
      this.chips = chips;
      this.probs = probs;
    }

    public long getChips() {
      return chips;
    }

    public void setChips(long chips) {
      this.chips = chips;
    }

    public long getProbs() {
      return probs;
    }

    public void setProbs(long probs) {
      this.probs = probs;
    }
  }
}
